package com.ics.atable.chat.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * Caffeine本地缓存的配置类
 *
 * @author haohongbin
 */
@Configuration
@ConfigurationProperties(prefix = "proj.caffeine")
public class CaffeineConfig {

    /**
     * 初始容量
     */
    @Value("${proj.caffeine.initial-capacity}")
    private String initialCapacity;

    /**
     * 最大容量
     */
    @Value("${proj.caffeine.maximum-size}")
    private String maximumSize;

    /**
     * 过期时间
     */
    @Value("${proj.caffeine.expire-time}")
    private String expireTime;

    @Bean
    public Cache<String, String> localCache(){
        return Caffeine.newBuilder().initialCapacity(Integer.parseInt(initialCapacity))
                .maximumSize(Long.parseLong(maximumSize))
                // 缓存 5 分钟移除
                .expireAfterWrite(Long.parseLong(expireTime), TimeUnit.MINUTES)
                .build();
    }
}
